<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search_model extends CI_Model {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */



	public function getListData($pId,$keyword,$baseQuery)
	{


		//每页数据条目
		$pageItemCount=20;

		$show_page=7;

		$pageOffset=($show_page-1)/2;


		//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔关键词
		$keywords=preg_split('/\s+/',$keyword);

		$OUTPUT=array();


		$this->load->database();

		$offset=($pId-1)*$pageItemCount;

		//使用CI内置数据库查询方法，简化查询语句
		$this->db->select('`book_id` ,`title`,`small_cover`,`resume`,`authors`,`translators`');




		foreach($keywords as $key => $value ){
			$this->db->or_like('title',$value);
		}

		$this->db->where('status',1);

		$this->db->order_by('view_count','DESC');

		//获取查询出来的记录数
		$OUTPUT['recordCount']=$this->db->count_all_results('book_item_tb',FALSE);

		$OUTPUT['data']=$this->db->limit($pageItemCount,$offset)->get()->result_array();




		if(empty($OUTPUT['data'])){
			return NUll;
			exit;
		}


		foreach($OUTPUT['data'] as  $index =>  $item){
			if(!empty($item['authors'])){
				$OUTPUT['data'][$index]['authors']=json_decode($item['authors']);
			}

			if(!empty($item['translators'])){
				$OUTPUT['data'][$index]['translators']=json_decode($item['translators']);
			}
		}




		//总页数
		$OUTPUT['total_pages']=intval(ceil($OUTPUT['recordCount']/$pageItemCount));
		$OUTPUT['page']=intval($pId);
		$OUTPUT['show_page']=$show_page;
		$OUTPUT['pageOffset']=$pageOffset;
		$OUTPUT['keyword']=$keyword;

		if(is_array($keywords)&&count($keywords)){
			//$baseQuery['q']=implode('+',$keywords);
		}



		$baseQueryUrl='';
		foreach ($baseQuery as $key => $value) {
			$baseQueryUrl.=$key.'='.$value.'&';
		}

		/*
		echo $baseQueryUrl;
		$pattern = '/&$/';
		$baseQueryUrl=preg_replace($pattern,'',$baseQueryUrl);
		*/

		$OUTPUT['list_base_url']='/search?'.$baseQueryUrl;


		return $OUTPUT;
	}


	public function getByISBN($pId,$keyword,$baseQuery){
		//传进来原始的ISBN,处理成纯数字版本的ISBN
		$isbnCode=preg_replace('/ISBN|\-|：/i','',$keyword);

		//每页数据条目
		$pageItemCount=20;

		$show_page=7;

		$pageOffset=($show_page-1)/2;




		$OUTPUT=array();


		$this->load->database();

		$offset=($pId-1)*$pageItemCount;

		//使用CI内置数据库查询方法，简化查询语句
		$this->db->select('`book_id` ,`title`,`small_cover`,`resume`,`authors`,`translators`');

		//获取ISBN 等于纯数字或其他规范的情况

		$ISBN=array(
			$isbnCode,
			$keyword
		);

		$this->db->where('status',1);
		$this->db->where_in('isbn',$ISBN);
		$this->db->order_by('view_count','DESC');


		//获取查询出来的记录数
		$OUTPUT['recordCount']=$this->db->count_all_results('book_item_tb',FALSE);

		$OUTPUT['data']=$this->db->limit($pageItemCount,$offset)->get()->result_array();


		if(empty($OUTPUT['data'])){
			return NUll;
			exit;
		}


		foreach($OUTPUT['data'] as  $index =>  $item){
			if(!empty($item['authors'])){
				$OUTPUT['data'][$index]['authors']=json_decode($item['authors']);
			}

			if(!empty($item['translators'])){
				$OUTPUT['data'][$index]['translators']=json_decode($item['translators']);
			}
		}




		//总页数
		$OUTPUT['total_pages']=intval(ceil($OUTPUT['recordCount']/$pageItemCount));
		$OUTPUT['page']=intval($pId);
		$OUTPUT['show_page']=$show_page;
		$OUTPUT['pageOffset']=$pageOffset;
		$OUTPUT['keyword']=$keyword;



		$baseQueryUrl='';
		foreach ($baseQuery as $key => $value) {
			$baseQueryUrl.=$key.'='.$value.'&';
		}



		$OUTPUT['list_base_url']='/search?'.$baseQueryUrl;


		return $OUTPUT;
	}


}
